package com.tongyi.lizhishop.activitys;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.blankj.utilcode.util.SPUtils;
import com.tongyi.lizhishop.common.CommonObserver;
import com.tongyi.lizhishop.constants.Constants;
import com.tongyi.lizhishop.utils.AppStringUtils;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ItemViewDelegate;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import com.tongyi.lizhishop.R;
import com.tongyi.lizhishop.api.MessageApi;
import com.tongyi.lizhishop.beans.InfoFriendBean;
import com.tongyi.lizhishop.common.RecycleViewActivity;
import com.tongyi.lizhishop.managers.APIServiceManager;

import java.util.ArrayList;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class RecommendDetailActivity extends RecycleViewActivity implements View.OnClickListener, MultiItemTypeAdapter.OnItemClickListener {


    ArrayList<Object> itemDatas = new ArrayList<>();
    private MultiItemTypeAdapter multiItemTypeAdapter;
    private String twoid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getExtras();
        twoid = extras.getString("twoid");
        initTitleBarView(titlebar, "详情");
        initView();
        refreshLayout.autoRefresh();
    }

    @Override
    public void loadData() {

        APIServiceManager.getInstance().create(MessageApi.class)
                .infoFriend(SPUtils.getInstance().getString("userid"), Constants.perPageSize, twoid, currentPageIndex)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CommonObserver<InfoFriendBean>() {
                    @Override
                    public void onSuccess(InfoFriendBean value) {
                        if (currentPageIndex == 0) {
                            itemDatas.clear();
                            itemDatas.add(value.getIphone());
                        }
                        calculatePageIndexAndControlRefreshLayout(value.getArray());
                        itemDatas.addAll(value.getArray());
                        multiItemTypeAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                        showError("请检查网络");
                    }
                });


    }

    private static final String TAG = "AlertMessageActivity";

    private void initView() {
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview.setItemAnimator(new DefaultItemAnimator());
        recyclerview.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL) {
                                           @Override
                                           public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                                               int childAdapterPosition = parent.getChildAdapterPosition(view);
                                               if (childAdapterPosition == 0) {
                                                   outRect.set(0, 0, 0, 0);
                                               } else {
                                                   super.getItemOffsets(outRect, view, parent, state);

                                               }
                                           }

                                       }
        );
        multiItemTypeAdapter = new MultiItemTypeAdapter(this, itemDatas);
        multiItemTypeAdapter.addItemViewDelegate(new ItemViewDelegate() {
            @Override
            public int getItemViewLayoutId() {
                return R.layout.activity_my_recommand_detail_item;
            }

            @Override
            public boolean isForViewType(Object item, int position) {
                return position == 0;
            }

            @Override
            public void convert(ViewHolder holder, Object o, int position) {
                String iphone = o.toString();
                holder.setText(R.id.telephon, iphone);
            }
        });
        multiItemTypeAdapter.addItemViewDelegate(new ItemViewDelegate() {
            @Override
            public int getItemViewLayoutId() {
                return R.layout.activity_my_recommand_detail_item_common;
            }

            @Override
            public boolean isForViewType(Object item, int position) {
                return position != 0;
            }

            @Override
            public void convert(ViewHolder holder, Object o, int position) {
                InfoFriendBean.ArrayBean arrayBean = (InfoFriendBean.ArrayBean) o;
                holder.setText(R.id.userPhone, AppStringUtils.getInfoFriendMoney(arrayBean)); //+2.56元
                holder.setText(R.id.content, arrayBean.getSco_content());
                holder.setText(R.id.time, AppStringUtils.getSafeTime("yyyy-MM-dd", arrayBean.getTime()));
            }
        });
        recyclerview.setAdapter(multiItemTypeAdapter);
        multiItemTypeAdapter.setOnItemClickListener(this);
    }


    @Override
    public void onClick(View v) {

    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        if (position == 0) {
        } else {

        }
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }
}
